<template>
  <div ref="container" class="carousel-item-container" @mousemove="mousemove">
    <div class="carousel-img" :style="imgPosition" ref="img">
      <ImgLoader
        :src="carousel.bigImg"
        :placeholder="carousel.midImg"
        @load="this.showText"
      ></ImgLoader>
    </div>
    <div ref="title" class="title">{{ carousel.title }}</div>
    <div ref="desc" class="desc">{{ carousel.description }}</div>
  </div>
</template>

<script>
import ImgLoader from "@/components/ImageLoader";
export default {
  props: ["carousel"],
  components: { ImgLoader },
  data: () => ({
    titleWidth: 0,
    descWidth: 0,
    containerSize: null, //外层容器尺寸
    innerSize: null, //里层图片放大的尺寸
    mouseX: 0, // 鼠标位置， 相对与容器
    mouseY: 0,
  }),
  mounted() {
    this.titleWidth = this.$refs.title.clientWidth;
    this.descWidth = this.$refs.desc.clientWidth;
    this.setSize();
    this.mouseX = this.center.x;
    this.mouseY = this.center.y;
    window.addEventListener("resize", this.setSize);
  },
  destroyed() {
    window.removeEventListener("resize", this.setSize);
  },
  computed: {
    center() {
      return {
        x: this.containerSize.width / 2,
        y: this.containerSize.height / 2,
      };
    },
    imgPosition() {
      if (!this.innerSize || !this.containerSize) return {};
      // 多出的宽度
      const extraWidth = this.innerSize.width - this.containerSize.width;
      const extraHeight = this.innerSize.height - this.containerSize.height;
      // 宽度差距 /
      const left = (-extraWidth / this.innerSize.width) * this.mouseX;
      const top = (-extraHeight / this.innerSize.height) * this.mouseY;
      return {
        transform: `translate(${left}px,${top}px)`,
      };
    },
  },
  methods: {
    mousemove(e) {
      const rect = this.$refs.container.getBoundingClientRect();
      this.mouseX = e.clientX - rect.left;
      this.mouseY = e.clientY - rect.top;
    },
    setSize() {
      this.containerSize = {
        width: this.$refs.container.clientWidth,
        height: this.$refs.container.clientHeight,
      };
      this.innerSize = {
        width: this.$refs.img.clientWidth,
        height: this.$refs.img.clientHeight,
      };
    },
    showText() {
      this.showWords(this.$refs.title, this.titleWidth);
      this.showWords(this.$refs.desc, this.descWidth);
    },
    showWords(dom, w) {
      dom.style.opacity = 1;
      dom.style.width = 0;
      // 强制渲染
      this.$refs.desc.clientWidth;
      dom.style.transition = "2s";
      dom.style.width = w + "px";
    },
  },
};
</script>

<style lang='less' scoped>
@import "~@/assets/style/var.less";
.carousel-item-container {
  background: @dark;
  width: 110%;
  height: 110%;
  position: relative;
  overflow: hidden;
  .carousel-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 110%;
    height: 110%;
    transition: 0.3s;
  }
  .title,
  .desc {
    position: absolute;
    left: 30px;
    color: #fff;
    text-shadow: 1px 0 0 rgba(0, 0, 0, 0.5), -1px 0 0 rgba(0, 0, 0, 0.5),
      0 1px 0 rgba(0, 0, 0, 0.5), 0 -1px 0 rgba(0, 0, 0, 0.5);
    white-space: nowrap;
    overflow: hidden;
    opacity: 0;
  }
  .title {
    top: calc(50% - 40px);
    font-size: 2em;
  }
  .desc {
    top: calc(50% + 20px);
    font-size: 1.2em;
    color: lighten(@gray, 20%);
  }
}
</style>
